package com.hellpro.lolbox.home.weight;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.bean.ZXTab;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.factory.ZXFragmentFactory;
import com.hellpro.lolbox.home.presenter.ZXTabBiz;
import com.hellpro.lolbox.home.presenter.ZXVpAdapter;
import com.hellpro.lolbox.home.view.IZXNews;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Package_Name: com.hellpro.lolbox.home.weight
 * Description :
 * author: HellPermanent
 * date:  2016/10/19 21:20
 * remarks：
 */
public class ZXActivity extends LolBaseActivity implements IZXNews {


    @BindView(R.id.home_zxtoolbar)
    Toolbar homeZxtoolbar;
    @BindView(R.id.home_zxvp)
    ViewPager homeZxvp;
    @BindView(R.id.home_zxtab)
    TabLayout homeZxtab;
    private ZXVpAdapter homeZxAdapter;
    private List<Fragment> fragmentList;
    private List<String> tabTitle;
    private static int TT = 0;
    private static int LZ = 3;
    private ZXTabBiz zxTabBiz;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_zx;
    }

    @Override
    public void initView() {
        homeZxtoolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //资讯Vp
        homeZxAdapter = new ZXVpAdapter(getSupportFragmentManager());
        homeZxvp.setAdapter(homeZxAdapter);
        homeZxvp.setOffscreenPageLimit(6);
        settabConfig();
        zxTabBiz = new ZXTabBiz(this, this);
        String zxtab = UrlConfigs.ZXTAB;
        zxTabBiz.loadTab(zxtab);
    }


    @Override
    public void settabConfig() {
        homeZxtab.setTabMode(TabLayout.MODE_FIXED);
        homeZxtab.setupWithViewPager(homeZxvp);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)

    public void getData(List<ZXTab> data) {

        getFragment(data);
        homeZxAdapter.setData(tabTitle, fragmentList);

    }

    @Override
    public void getFragment(List<ZXTab> zxTab) {
        fragmentList = new ArrayList<>();
        tabTitle = new ArrayList<>();

        int TT = 0;
        int LZ = 3;
        //创建
        for (ZXTab tab : zxTab) {
            String type = tab.getType();
            tabTitle.add(tab.getName());
            if (type.equals("album")) {
                Fragment fragment = ZXFragmentFactory.creatAFragment(LZ);
//                Bundle bundle1 = new Bundle();
//                bundle1.putString("path",UrlConfigs.ZXJT);
//                fragment.setArguments(bundle1);
                LZ++;
                fragmentList.add(fragment);
            } else {
                Fragment fragment = ZXFragmentFactory.creatAFragment(TT);
                TT++;

                fragmentList.add(fragment);
            }


        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
